#include "../include/myserver.h"

myserver::myserver():server()
{
    this->val = 0;
    this->step = 0;
    this->l = NULL;
    this->id = -1;
}

myserver::~myserver()
{
    this->val = 0;
    this->step = 0;
    this->l = NULL;
    this->id = -1;
}
void myserver::myservermakevaluegame()
{
    srand(time(NULL));
    for(int i=0; i<50; i++)
    {
        let[i].setall(i,0);
    }
    //tao bao thu 10.000.000->value = 10000
    int num = 0;
    int vl;
    while(num < 2)
    {
again1:
        vl = rand()%50;
        if(let[vl].getvalue() != 0)
        {
            goto again1;
        }
        else
        {
            let[vl].setvalue(10000);
            num++;
        }
    }
    num = 0;
    while(num < 2)
    {
again2:
        vl = rand()%50;
        if(let[vl].getvalue() != 0)
        {
            goto again2;
        }
        else
        {
            let[vl].setvalue(5000);
            num++;
        }
    }
    num = 0;
    while(num < 4)
    {
again3:
        vl = rand()%50;
        if(let[vl].getvalue() != 0)
        {
            goto again3;
        }
        else
        {
            let[vl].setvalue(1000);
            num++;
        }
    }
    num = 0;
    while(num < 4)
    {
again4:
        vl = rand()%50;
        if(let[vl].getvalue() != 0)
        {
            goto again4;
        }
        else
        {
            let[vl].setvalue(500);
            num++;
        }
    }
    num = 0;
    while(num < 4)
    {
again5:
        vl = rand()%50;
        if(let[vl].getvalue() != 0)
        {
            goto again5;
        }
        else
        {
            let[vl].setvalue(100);
            num++;
        }
    }
    num = 0;
    while(num < 4)
    {
again6:
        vl = rand()%50;
        if(let[vl].getvalue() != 0)
        {
            goto again6;
        }
        else
        {
            let[vl].setvalue(50);
            num++;
        }
    }
    num = 0;
    while(num < 2)
    {
again7:
        vl = rand()%50;
        if(let[vl].getvalue() != 0)
        {
            goto again7;
        }
        else
        {
            let[vl].setvalue(-100);
            num++;
        }
    }
    num = 0;
    while(num < 4)
    {
again8:
        vl = rand()%50;
        if(let[vl].getvalue() != 0)
        {
            goto again8;
        }
        else
        {
            let[vl].setvalue(-50);
            num++;
        }
    }
    num = 0;
    while(num < 4)
    {
again9:
        vl = rand()%50;
        if(let[vl].getvalue() != 0)
        {
            goto again9;
        }
        else
        {
            let[vl].setvalue(-25);
            num++;
        }
    }
}
void myserver::myserverstart()
{
    if(serverinit() == 1)
    {
        cout<<"Init successful ."<<endl;
        if(servercreatesocket() == 1)
        {
            cout<<"Create socket succesfull ."<<endl;
            if(serverbind() == 1)
            {
                cout<<"Bind successful ."<<endl;
                if(serverlisten() == 1)
                {
                    cout<<"Server listenning ... "<<endl;
                    if(serveraccept() == 1)
                    {
                        cout<<"Client connected ."<<endl;
                        while(1)
                        {

                            myservermakevaluegame();
                            myserverprocessgame();
                            myserverprocessfinish();

                        }
                    }
                    else
                    {
                        serverclose();
                        cout<<"Accept fail ."<<endl;
                        cin.get();
                        cin.get();
                        exit(-5);
                    }
                }
                else
                {
                    serversocketclose();
                    cout<<"Listen fail ."<<endl;
                    cin.get();
                    cin.get();
                    exit(-4);
                }
            }
            else
            {
                servercleanup();
                cout<<"Bind fail ."<<endl;
                cin.get();
                cin.get();
                exit(-3);
            }
        }
        else
        {
            servercleanup();
            cout<<"Create socket fail ."<<endl;
            cin.get();
            cin.get();
            exit(-2);
        }

    }
    else
    {
        servercleanup();
        cout<<"Winsock init fail ."<<endl;
        cin.get();
        cin.get();
        exit(-1);
    }

}
void myserver::myserverprocessgame()
{
    myservergetparameters();
    string ques,an1,an2,an3,an4;
    int antrue;
    int st = 0;
    while(st < step)
    {

        sen.datasetvalue(1,step,"","","","","",-1);
again:
        serversend();
        serverrecv();
        int re = rec.datagetx();
        while(myservercheckid(re) == -1)
        {
            sen.datasetvalue(-2,-1,"","","","","",-1);
            goto again;
        }
        cin.ignore(1);
        cout<<"Question : ";
        getline(cin,ques,'\n');
        cout<<endl;
        cout<<"Answer 1 : ";
        getline(cin,an1,'\n');
        cout<<endl;
        cout<<"Answer 2 : ";
        getline(cin,an2,'\n');
        cout<<endl;
        cout<<"Answer 3 : ";
        getline(cin,an3,'\n');
        cout<<endl;
        cout<<"Answer 4 : ";
        getline(cin,an4,'\n');
        cout<<endl;
        antrue = 5;
        while(antrue <1 || antrue > 4)
        {
            cout<<"Answer true <1-4> : ";
            cin>>antrue;
            cout<<endl;
        }

        sen.datasetvalue(2,-1,ques,an1,an2,an3,an4,-1);
        serversend();
        serverrecv();
        myservercheckresult(re,antrue);
        st++;


    }

}
int myserver::myservercheckid(int idd)
{
    if(idd < 0 || idd > 49)
    {
        return -1;
    }

    else
    {

            for(int i = 0; i<=id; i++)
            {
                if(l[i] == idd)
                {
                    return -1;
                }
            }
            return 1;
    }
}
int myserver::myservercheckresult(int idd,int anstrue)
{
    int result = rec.datagetx();
    if(myservercheckid(idd) == 1)
    {

        if(result == anstrue)
        {
            id++;
            l[id] = idd;

        }
        return 1;
    }
    else
    {
        return -1;
    }
}
void myserver::myservergetparameters()
{
    id = -1;
    l = NULL;
    this->val = 0;
    this->step = 0;
    while(step< 1 || step > 5)
    {
        cout<<"So bao thu client duoc mo <1-5>:";
        cin>>this->step;
        cout<<endl;
    }
    l = new int[step];
    for(int i=0 ; i<step; i++)
    {
        l[i] = -1;
    }
}
void myserver::myserverprocessfinish()
{
    int choo;
    if(id >= 0)
    {
        if(id == 0)
        {
            //chi co 1 bao thu
            choo = 1;
        }
        else if(id < 2 )
        {
            choo = 2;

        }
        else
        {
            //du ca
            choo = rand()%3+1;
        }
        int price;
        switch(choo)
        {
        case 1:
            price = 5;
            sen.datasetvalue(3,1,"","","","","",price);
            break;
        case 2:
            price = 10;
            sen.datasetvalue(3,2,"","","","","",price);
            break;
        case 3:
            price = 5 + val;
            sen.datasetvalue(3,3,"","","","","",price);
            break;
        }
        //gui gia muon mua
        serversend();
        //nhan tra loi
        serverrecv();
        int yesno = rec.datagetx();
        if(yesno == 1)
        {
            val =+ price;

        }
        else
        {
            val =+ rec.datagetansertrue();

        }
        //xoa bao thu da ban
        if(choo == 1 || choo == 3)
        {

                myserverremoveid(id);
        }
        else
        {
            myserverremoveid(id);
            myserverremoveid(id);

        }

        //gui cap nhat gia ti cua client
        sen.datasetvalue(4,val,"","","","","",-1);
        serversend();
        //mua ban trao doi
        //1 bao thu
        //2 2 bao thu
        //3 la bao thu + ngan phieu

        // tinh gia tri nhung bao thu con
        if(id >= 0)
        {
            for(int t=0;t<id;t++)
            {
               int yourval =  let[l[t]].getvalue();
               if(yourval > 0)
               {
                   val = val + yourval;
               }
               else
               {
                   if(yourval == -100)
                   {
                       val = 0;
                   }
                   else if(yourval == -50)
                   {
                       val = val/2;
                   }
                   else
                   {
                       val = val/4;
                   }

               }
            }
            sen.datasetvalue(4,val,"","","","","",-1);
            serversend();
        }
        //cho xem co ket thuc k?
        serverrecv();
        if(rec.datagettype() == 0)
        {
            //ket thuc;
            cout<<"Client muon ket thua game ."<<endl;
            sen.datasetvalue(0,-1,"","","","","",-1);
            serversend();
            serverclose();
            cin.get();cin.get();
            exit(0);
        }

    }
    else
    {
        //thoat chuong trinh
            cout<<"Game ket thua ."<<endl;
            sen.datasetvalue(0,-1,"","","","","",-1);
            serversend();
            serverclose();
            cin.get();cin.get();
            exit(0);
        //break;
    }

}
void myserver::myserverremoveid(int rmid)
{
    //bo cai cuoi cung
   id--;
}
